package com.cssw.fyzb.busi.service.im;

import com.alibaba.fastjson.JSON;
import com.cssw.fyzb.busi.config.TpsClient;
import com.cssw.fyzb.core.dao.user.UserDao;
import com.cssw.fyzb.core.dao.video.UserVideoDao;
import com.cssw.fyzb.core.dao.video.VideoCommentDao;
import com.cssw.fyzb.core.eao.video.*;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.core.model.dodb.CmfVideoComment;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author Jian Qin.
 */
@Service
public class ImServiceImpl implements ImService {
    @Autowired
    private TpsClient tpsClient;

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserVideoDao videoDao;

    @Autowired
    private VideoCommentDao videoCommentDao;

    @Autowired
    private NotifyEao notifyEao;

    @AllArgsConstructor
    @Data
    public class ImNotify {
        String cmd;
        String tips;

        public String data() {
            return JSON.toJSONString(this);
        }
    }


    @Override
    public void importAccount(Long userId) {
        User user = userDao.getById(userId);
        Request request = new Request("AccountImport");
        request.setParameter("identifier", userId);
        request.setParameter("nick", user.getNickName());
        request.setParameter("faceUrl", user.getAvatar());
        tpsClient.send(request);
    }

    @Override
    public void sendLikeCommentMsg(Long userId, Long commentId) {
        CmfVideoComment comment = videoCommentDao.findById(commentId);
        CmfUserVideo video = videoDao.findById(comment.getVideoId());
        User user = userDao.getById(userId);
        ImNotify notify = new ImNotify("like", user.getNickName() + "点赞了我的评论");
        Request request = new Request("SendCustomMsg");
        request.setParameter("from_Account", userId);
        request.setParameter("to_Account", comment.getUserId());
        request.setParameter("data", notify.data());
        request.setParameter("desc", "");
        request.setParameter("ext", "");
        request.setParameter("sound", "");
        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, comment.getToUserId(), notify.getCmd(), notify.getTips(),
//                    null, video.getVideoId(), video.getThum(), new Date());
//        }

    }

    @Override
    public void sendCancelLikeCommentMsg(Long userId, Long commentId) {
        CmfVideoComment comment = videoCommentDao.findById(commentId);
        CmfUserVideo video = videoDao.findById(comment.getVideoId());
        User user = userDao.getById(userId);
        ImNotify notify = new ImNotify("cancelLike", user.getNickName() + "取消了对我的评论的点赞");
        Request request = new Request("SendCustomMsg");
        request.setParameter("from_Account", userId);
        request.setParameter("to_Account", comment.getUserId());
        request.setParameter("data", notify.data());
        request.setParameter("desc", "");
        request.setParameter("ext", "");
        request.setParameter("sound", "");
        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, comment.getToUserId(), notify.getCmd(), notify.getTips(),
//                    null, video.getVideoId(), video.getThum(), new Date());
//        }

    }

    @Override
    public void sendLikeVideoMsg(Long userId, Long videoId) {
//        CmfUserVideo video = videoDao.findById(videoId);
//        User user = userDao.getById(userId);
//        ImNotify notify = new ImNotify("like", user.getNickName() + "点赞了我的视频");
//        Request request = new Request("SendCustomMsg");
//        request.setParameter("from_Account", userId);
//        request.setParameter("to_Account", video.getUserId());
//        request.setParameter("data", notify.data());
//        request.setParameter("desc", "");
//        request.setParameter("ext", "");
//        request.setParameter("sound", "");
//        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, video.getUserId(), notify.getCmd(), notify.getTips(),
//                    null, video.getVideoId(), video.getThum(), new Date());
//        }
    }

    @Override
    public void sendCancelLikeVideoMsg(Long userId, Long videoId) {
//        CmfUserVideo video = videoDao.findById(videoId);
//        User user = userDao.getById(userId);
//        ImNotify notify = new ImNotify("cancelLike", user.getNickName() + "取消了对我的视频的点赞");
//        Request request = new Request("SendCustomMsg");
//        request.setParameter("from_Account", userId);
//        request.setParameter("to_Account", video.getUserId());
//        request.setParameter("data", notify.data());
//        request.setParameter("desc", "");
//        request.setParameter("ext", "");
//        request.setParameter("sound", "");
//        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, video.getUserId(), notify.getCmd(), notify.getTips(),
//                    null, video.getVideoId(), video.getThum(), new Date());
//        }

    }

    @Override
    public void sendAddCommentMsg(Long userId, Long commentId) {
//        CmfVideoComment comment = videoCommentDao.findById(commentId);
//        CmfUserVideo video = videoDao.findById(comment.getVideoId());
//        User user = userDao.getById(userId);
//        ImNotify notify = new ImNotify("comment", user.getNickName() + "评论了我的视频");
//        Request request = new Request("SendCustomMsg");
//        request.setParameter("from_Account", userId);
//        request.setParameter("to_Account", comment.getToUserId());
//        request.setParameter("data", notify.data());
//        request.setParameter("desc", "");
//        request.setParameter("ext", "");
//        request.setParameter("sound", "");
//        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, comment.getToUserId(), notify.getCmd(), notify.getTips(),
//                    comment.getContent(), video.getVideoId(), video.getThum(), new Date());
//        }

    }

    @Override
    public void sendAtMeMsg(Long userId, Long commentId, Long videoId, String content) {
//        User user = userDao.getById(userId);
//        CmfVideoComment comment = null;
//        CmfUserVideo video = null;
//        Long toUserId = null;
//
//        if (commentId != null) {
//            comment = videoCommentDao.findById(commentId);
//            video = videoDao.findById(comment.getVideoId());
//            toUserId = comment.getToUserId();
//
//        } else if (videoId != null) {
//            video = videoDao.findById(videoId);
//            toUserId = video.getUserId();
//        }
//        if (comment == null && video == null) {
//            return;
//        }

//        ImNotify notify = new ImNotify("at", user.getNickName() + "@我");
//        Request request = new Request("SendCustomMsg");
//        request.setParameter("from_Account", userId);
//        request.setParameter("to_Account", video.getUserId());
//        request.setParameter("data", notify.data());
//        request.setParameter("desc", "");
//        request.setParameter("ext", "");
//        request.setParameter("sound", "");
//        Response response = tpsClient.send(request);

//        if(response.isSuccess()){
//            notifyEao.save(userId, toUserId, notify.getCmd(), notify.getTips(),
//                    content, video.getVideoId(), video.getThum(), new Date());
//        }

    }

    @Override
    public void sendFocusMeMsg(Long userId, Long focusUserId) {
        User user = userDao.getById(userId);
        ImNotify notify = new ImNotify("focus", user.getNickName() + "粉了我");
        Request request = new Request("SendCustomMsg");
        request.setParameter("from_Account", userId);
        request.setParameter("to_Account", focusUserId);
        request.setParameter("data", notify.data());
        request.setParameter("desc", "");
        request.setParameter("ext", "");
        request.setParameter("sound", "");
        Response response = tpsClient.send(request);

        if (response.isSuccess()) {
            notifyEao.save(userId, focusUserId, notify.getCmd(), notify.getTips(),
                    user.getSign(), null, null, new Date());
        }

    }
}
